package leetcode.L131;

import java.util.ArrayList;
import java.util.List;

class Solution {

    List<List<String>> res = new ArrayList<>();
    List<String> curr = new ArrayList<>();

    public List<List<String>> partition(String s) {
        backTrack(s, 0);
        return res;
    }

    //分割的字符串，下次分割的位置
    private void backTrack(String s, int start) {
        //回溯+尝试分割
        if (start == s.length()) {
            res.add(new ArrayList<>(curr));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            if (judge(s, start, i)) {//是一个回文串
                curr.add(s.substring(start, i + 1));
                backTrack(s, i + 1);
                curr.remove(curr.size() - 1);
            }
        }
    }

    //判断是否是回文串
    private Boolean judge(String s, int i, int j) {
        while (i < j) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;
                j--;
            } else {
                return false;
            }
        }
        return true;
    }
}